<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all">
</head>
<body>

<%--添加出库信息--%>
<div class="layui-row layui-col-space15">
    <div class="layui-col-md4">
        <div class="grid-demo grid-demo-bg1">
            <form class="layui-form" lay-filter="addOrUpdate" id="addOrUpdate">
                <%--<input type="hidden" name="id" placeholder="请输入id" autocomplete="off" class="layui-input">--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">书籍类型</label>
                    <div class="layui-input-inline">
                        <select name="typename" lay-filter="selType" id="selType" lay-filter="aihao">
                            <option value="">请选择书籍类型</option>
                        </select>
                        <input type="hidden" name="tname" id="pType">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">书名</label>
                    <div class="layui-input-inline">
                        <select name="bookname" lay-filter="selBook"  id="selBook" lay-filter="aihao">
                            <option value="">请选择书籍</option>
                        </select>
                        <input type="hidden" name="bname" id="pBook">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">出版社</label>
                    <div class="layui-input-inline">
                        <select name="publisher"  lay-filter="selPub" id="selPub" lay-filter="aihao">
                            <option value="">请选择出版社</option>
                        </select>
                        <input type="hidden" name="pname" id="pPub">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="author" readonly id="author" placeholder="请输入作者名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-inline">
                        <input type="text" name="count"  id="count" placeholder="请输入数量" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">单价</label>
                    <div class="layui-input-inline">
                        <input type="text" name="outprice" id="danjia" readonly  placeholder="请输入单价" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="stChuKu">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-col-md8">
        <div class="grid-demo">
            <table class="layui-hide" id="test" lay-filter="test"></table>

            <div class="layui-form-item">
                <label class="layui-form-label">总价格</label>
                <div class="layui-input-inline">
                    <input type="text" name="totalprice" readonly id="totalprice"  autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">总数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="totalcount" readonly id="totalcount"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">经手人</label>
                <div class="layui-input-inline">
                    <input type="text" name="jsr" id="jsr"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" id="ruKuTJ" lay-submit lay-filter="ruKuTJ">立即提交</button>
                </div>
            </div>
        </div>
    </div>
</div>


<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="deleteAll">删除全部</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${pageContext.request.contextPath}/layui/layui.all.js" charset="utf-8"></script>

<script>
    layui.use(['table','form','layer','jquery','upload','util'], function(){
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        var util = layui.util;

        var chukuxinxi = [];
        function showtable(){
            table.render({
                elem: '#test'
                ,data:chukuxinxi
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,title: '用户数据表'
                ,cols: [[
                    /*{type: 'checkbox', fixed: 'left'}
                    ,*/{field:'bname', title:'书名',fixed: 'left', unresize: true, sort: true}
                    ,{field:'tname', title:'书籍类型'}
                    ,{field:'author', title:'作者'}
                    ,{field:'pname', title:'出版社',sort: true}
                    ,{field:'count', title:'数量'}
                    ,{field:'outprice', title:'单价'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true
                ,limit:5
                ,limits:[5,10,20]
                ,even:true
            });
        }


        $(function () {
            showtable();
            showselecttype();
        })

        /*
        *  form 表单提交事件
        * */
        form.on('submit(stChuKu)', function(data){
            var flag = false;
            for (var i = 0; i < chukuxinxi.length; i++) {
                if (data.field.bookname == chukuxinxi[i].bookname && data.field.publisher == chukuxinxi[i].publisher) {
                    flag = true;
                    break;
                }
            }
            if (flag){
                layer.msg("已经存在相同的数据，只需要修改表格中的数量即可！！！");
            }else {
                var count = data.field.count;
                var bookid = data.field.bookname;
                var aa = 0;
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath}/book/selectPublisherAndBookid',
                    data:{"bookid":bookid},
                    async:false, //同步
                    dataType:'json',//预期的后台数据类型
                    success:function (data) {
                        for (var i = 0;i<data.length;i++){
                            if (data[i].count < count){
                                aa=data[i].count;
                            }else {
                                aa = count;
                            }
                        }
                    }
                })
                data.field.count = aa;
                chukuxinxi.push(data.field);
                table.reload('test', {
                    data:chukuxinxi
                });
                zong();
            }
            $("#addOrUpdate")[0].reset();
            return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //查询所有书籍类型，并填充到下拉框中
        function showselecttype(){
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/type/selectType',
                dataType:'json',//预期的后台数据类型
                success:function (data) {
                    //通过循环遍历查到的所有类型，并填充到类型下拉框中
                    var op="";
                    for (var i=0;i<data.length;i++){
                        op +="<option value='"+data[i].typeid+"'>"+data[i].typename+"</option>";
                    }
                    $("#selType").append(op);
                    form.render('select');//表单的更新渲染 下拉框
                }
            })
        }

        //查询所有的 书籍名称 添加到 下拉框中
        form.on('select(selType)', function(data){
            $("#selBook").html("<option>请选择书籍</option>");
            var typeid = data.value;
            var typename = $("#selType option:selected").text();
            $("#pType").val(typename);
            //alert(typename);
            //alert(typeid);
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/book/selectBookNameAndBookid',
                data:{"typeid":typeid},
                dataType:'json',//预期的后台数据类型
                success:function (data) {
                    //通过循环遍历查到的所有类型，并填充到类型下拉框中
                    var op="";
                    for (var i=0;i<data.length;i++){
                        op +="<option value='"+data[i].bookid+"'>"+data[i].bookname+"</option>";
                    }
                    $("#selBook").append(op);
                    form.render('select');//表单的更新渲染 下拉框
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //查询所有的 出版社的名字 添加到 下拉框中
        form.on('select(selBook)', function(data){
            $("#selPub").html("<option>请选择出版社</option>");
            var bookid = data.value;
            var bookname = $("#selBook option:selected").text();
            $("#pBook").val(bookname);
            //alert(bookid);
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/book/selectPublisherAndBookid',
                data:{"bookid":bookid},
                dataType:'json',//预期的后台数据类型
                success:function (data) {
                    //通过循环遍历查到的所有类型，并填充到类型下拉框中
                    var op="";
                    for (var i=0;i<data.length;i++){
                        op +="<option value='"+data[i].bookid+"'>"+data[i].publisher+"</option>";
                    }
                    $("#selPub").append(op);
                    form.render('select');//表单的更新渲染 下拉框
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //把出版社 的名字 添加进去
        form.on('select(selPub)', function(data){
            var publisher = $("#selPub option:selected").text();
            $("#pPub").val(publisher);
            var bookid = data.value;
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/book/selectPublisherAndBookid',
                data:{"bookid":bookid},
                dataType:'json',//预期的后台数据类型
                success:function (data) {
                    $("#author").val(data[0].author);
                    $("#danjia").val(data[0].price);
                    $("#count").val(data[0].count);
                    //alert(JSON.stringify(data));
                }
            })
            //alert(bookid);
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        function zong(){
            var zongjia = 0;
            var zongliang = 0;
            for (var i = 0;i<chukuxinxi.length;i++){
                zongjia += Number(chukuxinxi[i].count*chukuxinxi[i].outprice);
                zongliang += Number(chukuxinxi[i].count);
            }
            $("#totalprice").val(zongjia);
            $("#totalcount").val(zongliang);
        }

        $("#ruKuTJ").click(function () {
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/chuKu/addChuKu',
                data:{"totalprice":$("#totalprice").val(),"totalcount":$("#totalcount").val(),"jsr":$("#jsr").val(),"chukuxinxi":JSON.stringify(chukuxinxi)},
                dataType:'json',//预期的后台数据类型
                success:function (data) {
                    if (data.code == "1001"){
                        layer.alert(data.msg, {icon: 1});
                        chukuxinxi = [];
                        table.reload('test', {
                            data:chukuxinxi
                        });
                        zong();
                    }else {
                        layer.alert(data.msg, {icon: 1});
                    }

                }
            })
        })

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'deleteAll':
                    chukuxinxi = [];
                    table.reload('test', {
                        data:chukuxinxi
                    });
                    zong();
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                //alert(JSON.stringify(data));
                var arr = chukuxinxi.lastIndexOf(data);
                //alert(arr);
                chukuxinxi.splice(arr,1);
                //alert(JSON.stringify(chukuxinxi));
                table.reload('test', {
                    data:chukuxinxi
                });
                zong();
            }
        });
    });
</script>

</body>
</html>
